package com.ddlottery.service;

import com.ddlottery.dao.DDbusinessMapper;
import com.ddlottery.dao.DDuserMapper;
import com.ddlottery.model.DDbusiness;
import com.ddlottery.model.DDorder;
import com.ddlottery.dao.DDorderMapper;
import com.ddlottery.tools.tools;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import com.ddlottery.analytic.jczq;
import com.ddlottery.analytic.jclq;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by ElNino on 16/1/22.
 */
@Service
public class DDorderService {

    @Autowired
    DDorderMapper DDorderMapper;

    @Autowired
    DDbusinessService DDbusinessService;

    @Autowired
    DDuserMapper DDuserMapper;

    @Autowired
    DDbusinessMapper DDbusinessMapper;

    @Autowired
    jczq jczq;

    @Autowired
    jclq jclq;

    public Integer addorder(DDorder order){
        order.setCreattime(new Date());
        Integer odernum = DDorderMapper.bidCount(order.getBid());
        odernum = odernum+1;
        order.setOrdernum(odernum.toString());
        DDorderMapper.insertSelective(order);
        return 0;
    }

    public String pcoder(DDorder order){
        order.setCreattime(new Date());
        Integer odernum = DDorderMapper.bidCount(order.getBid());
        odernum = odernum+1;
        String oid = odernum.toString();
        if(odernum<10){
            oid="000"+oid;
        }else if(odernum<100){
            oid="00"+oid;
        }else if(odernum<1000){
            oid="0"+oid;
        }
        order.setOrdernum(oid);
        DDorderMapper.insertSelective(order);
        return oid;
    }

    public Map orderList(DDorder order,Integer page){
        Map<String,Object> map = new HashMap<String, Object>();
        PageBounds pageBounds = new PageBounds(page,20);
        ArrayList<DDorder> list = DDorderMapper.selectByOther(order, pageBounds);
        PageList pageList = (PageList)list;
        map.put("code",0);
        map.put("list",list);
        map.put("count",pageList.getPaginator().getTotalCount());
        map.put("pagesize",20);
        map.put("page",page);
        return map;
    }

    public Map orderNow(DDorder order,Integer page){
        Map<String,Object> map = new HashMap<String, Object>();
        PageBounds pageBounds = new PageBounds(page,20);
        ArrayList<DDorder> list = DDorderMapper.selectByNow(order, pageBounds);
        PageList pageList = (PageList)list;
        map.put("code",0);
        map.put("list",list);
        map.put("count",pageList.getPaginator().getTotalCount());
        map.put("pagesize",20);
        map.put("page",page);
        return map;
    }

    public Integer userPrintOrder(Integer oid, Integer bid){
        DDorder order = DDorderMapper.selectByPrimaryKey(oid);
        if(order.getClosetime().getTime()<new Date().getTime()){
            return 1;
        }
        order.setBid(bid);
        order.setState((byte)2);
        DDorderMapper.updateByPrimaryKeySelective(order);
        return 0;
    }

    public Map orderInfo(Integer oid) throws IOException {
        Map<String,Object> map = new HashMap<String, Object>();
        DDorder order = DDorderMapper.selectByPrimaryKey(oid);
        String matchs = order.getContent();
        tools tools = new tools();
        map.put("code",0);
        map.put("info",order);
        map.put("nowtime",new Date());
        if(order.getLot()==1) {
            List matchList = tools.getMatch(matchs);
            map.put("match", matchList);
        }else if (order.getLot()==35) {
            List matchList = tools.getLqMatch(matchs);
            map.put("match", matchList);
        }
        return map;
    }

    public Map machineGetOrder(Integer bid , String md5Code) throws Exception {
        Map<String,Object> map = new HashMap<String, Object>();
        String md5 = DigestUtils.md5Hex(bid.toString() + "123456");
        if(md5.equals(md5Code) || md5Code.equals("cym")){
            DDorderMapper.machUpdateList(bid);
            DDbusiness businee = DDbusinessMapper.selectByPrimaryKey(bid);
            Integer machType = businee.getMachtype();
            ArrayList<DDorder> list = DDorderMapper.machOrderList(bid);
            List matchList = new ArrayList();
            if(list.get(0).getLot() == 1) {
                matchList = jczq.getMatchList(list);
            }else{
               matchList = jclq.getMatchList(list);
            }
            for (DDorder info : list) {
                if(info.getLot() == 1) {
                    info = jczq.getInfo(info, matchList,machType);
                }
                if(info.getLot() == 35){
                    info = jclq.getInfo(info, matchList,machType);
                }
            }
            map.put("code",0);
            map.put("list",list);
            return map;
        }else {
            map.put("code", -1);
            return map;
        }
    }

    public Integer machinePrintOrder(Integer oid, String md5Code){
        String str = DigestUtils.md5Hex(oid.toString() + "123456");
        if(str.equals(md5Code)){
            DDorder order = new DDorder();
            order.setOid(oid);
            order.setIsprint((byte)1);
            order.setState((byte)4);
            order.setPrinttime(new Date());
            DDorderMapper.updateByPrimaryKeySelective(order);
            return 0;
        }else {
            return 1;
        }
    }


}
